/**
 * 
 */
package cn.edu.thsis.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.thsis.dao.NewsInfoDao;
import cn.edu.thsis.pojo.User;
import cn.edu.thsis.pojo.mess;
import cn.edu.thsis.pojo.newsinfo;

/**
 * Title:   NewsInfoDaoImpl.java
 * Package: cn.edu.thsis.dao.impl
 * @Description:TODO
 * @author: Hui
 * @date:   2019年5月17日下午3:00:20
 * @version: V1.0
 * @Copyright:2019
 */
public class NewsInfoDaoImpl extends BaseDao implements NewsInfoDao {

	@Override
	public List<newsinfo> getNewsInfoList() throws SQLException {
				Connection conn = null;
				PreparedStatement ps = null;
				ResultSet rs = null;
				List<newsinfo> list = new ArrayList<newsinfo>();
				try {
					conn = jdbcUtils.getConnection();
					String sql = "select * from newsinfo";
					ps=conn.prepareStatement(sql);
					rs=ps.executeQuery();
					newsinfo ni = null;
					  while (rs.next()) {
						ni = new newsinfo(rs.getString("nid"), rs.getString("title"), rs.getString("summary"), rs.getString("content"), rs.getString("author"), rs.getDate("createDate"));
						list.add(ni);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
						if(rs != null) rs.close();
						if(ps != null) ps.close();
						if(conn != null) conn.close();
						}
				return list;
	}

	//新增公告
	@Override
	public boolean insert(newsinfo ni) {
		String sql = "insert into newsinfo values(?,?,?,?,?,?)";
		Object[] obj = {ni.getNid(),ni.getTitle(),ni.getSummary(),ni.getContent(),ni.getAuthor(),ni.getCreateDate()};
		return executeCUD(sql, obj)>0?true:false;
	}
	
	//修改公告
	@Override
	public boolean update(newsinfo ni) {
		String sql = "update from 'newsinfo' set 'title'=?,'summary'=?,'content'=?,'author'=? where nid=?";
		Object[] obj = {ni.getTitle(),ni.getSummary(),ni.getContent(),ni.getAuthor(),ni.getNid()};
		return executeCUD(sql, obj)>0?true:false;
	}
	
	//删除公告
	@Override
	public boolean delete(String nid) {
		String sql = "delete from newsinfo where nid=?";
		Object[] obj = {nid};
		return executeCUD(sql, obj)>0?true:false;
	}

	//公告详情
	@Override
	public newsinfo newcontDao(String nid) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		newsinfo newsinfo = null;
		try {
			conn = jdbcUtils.getConnection();
			String sql = "SELECT title,createDate,content from newsinfo WHERE nid =?";
			ps = conn.prepareStatement(sql);
			ps.setString(1,nid);
			rs = ps.executeQuery();
			while(rs.next()) {
				newsinfo = new newsinfo();
				newsinfo.setTitle(rs.getString("title"));
				newsinfo.setCreateDate(rs.getDate("createDate"));
				newsinfo.setContent(rs.getString("content"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return newsinfo;
	}

	//留言
	@Override
	public boolean messDao(mess me) {
		String sql = "insert into mess values(?,?,?,?)";
		Object[] obj = {me.getSid(),me.getTid(),me.getMid(),me.getWtime()};
		return executeCUD(sql, obj)>0?true:false;
	}

}
